﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class VertexAnimationData:ScriptableObject{
	public float fps;
	public List<VertexAnimationClip> animationClips = new List<VertexAnimationClip>();
	public Dictionary<string,VertexAnimationClip> dictClips = new Dictionary<string, VertexAnimationClip> ();
	public Material material;
	public Vector2[] uv;
	public int[] triangles;
	public float GetClipLength(string name){
		if (dictClips.ContainsKey (name))
			return dictClips [name].clipTime;
		else{
			Debug.LogWarning (" 找不到 动画" + name);
			return 1;
		}
	}
	public void Awake(){
		for (int i = 0; i < animationClips.Count; i++) {
			if(!dictClips.ContainsKey(animationClips [i].name))
				dictClips.Add (animationClips [i].name, animationClips [i]);
		}
	}
	public void Add(VertexAnimationClip clip){
		animationClips.Add (clip);
	}
	public Mesh CreateMesh(){
		Mesh mesh = new Mesh();
		mesh.vertices = animationClips [0].meshData [0].vertices;
		mesh.uv = uv;
		mesh.triangles = triangles;
		return mesh;
	}
	public void BackMesh(string name,ref Mesh mesh,float time){
		dictClips[name].BackMesh(ref mesh,time,fps);
	}
}

[System.Serializable]
public class VertexAnimationClip{
	public string name;
	public List<MeshData> meshData = new List<MeshData>();

	public float clipTime;
	public void BackMesh(ref Mesh mesh,float playtime,float fps = 30){
		float time = playtime%clipTime;
		int count;
		count = (int)(time*fps);
		if (count < meshData.Count) {
			mesh.vertices = meshData [count].vertices;
		}
	}
}
[System.Serializable]
public class MeshData{
	public Vector3[] vertices;

	public MeshData(Mesh mesh){
		vertices = mesh.vertices;
	}
}